package com.zyg.apart.controller;

import com.github.pagehelper.PageInfo;
import com.zyg.apart.model.Flats;
import com.zyg.apart.model.Lessee;
import com.zyg.apart.service.FlatsService;
import com.zyg.apart.service.LesseeService;
import com.zyg.apart.service.RectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.zyg.apart.model.Rect;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/rect")
public class RectController {

    @Autowired
    private RectService rectService;

    @Autowired
    private LesseeService lesseeService;

    @Autowired
    private FlatsService flatsService;

    @RequestMapping("/list")
    public String list(Integer currentPage, Model model){
        if (currentPage == null){
            currentPage = 1;
        }
        PageInfo<Map<String,Object>> rects = rectService.getAllRect(currentPage);
        model.addAttribute("rects",rects);
        return "rect/list";
    }

    @RequestMapping("/detail")
    public String detail(Model model,Integer id){
        Map<String,Object> rect = rectService.getRectById(id);
        model.addAttribute("rect",rect);
        return "rect/detail";
    }

    @RequestMapping("/toEdit")
    public String toEdit(Model model, Integer id){
        Map<String,Object> rect = rectService.getRectById(id);
        List<Lessee> lss = lesseeService.getLSNames();
        List<Flats> flats = flatsService.getAllName();
        model.addAttribute("rect",rect);
        model.addAttribute("lss",lss);
        model.addAttribute("flats",flats);
        return "rect/edit";
    }

    @RequestMapping("/edit")
    public String edit(Rect rect){
        System.out.println(rect);
        int i = rectService.editRect(rect);
        return "forward:list";
    }

    @RequestMapping("/toAdd")
    public String toAdd(Model model){
        List<Lessee> lss = lesseeService.getLSNames();
        List<Flats> flats = flatsService.getAllName();
        model.addAttribute("lss",lss);
        model.addAttribute("flats",flats);
        return "rect/add";
    }

    @RequestMapping("/add")
    public String add(Rect rect){
        System.out.println(rect);
        int i = rectService.addRect(rect);
        return "forward:list";
    }

    @RequestMapping("/del")
    public String del(Integer id){
        int i = rectService.delRectById(id);
        return "forward:list";
    }

}
